<?php


namespace App\Models\CacheModel\live;


use App\Models\Api\BaseCache;
use think\Log;

class LiveAward extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_award';

    protected $cacheModel = 'live.live_award';


    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('id', $ids)->column('name,type,status,cid', 'id');

            $gids = [];
            $cids = [];
            foreach ($data as $aId => $awardInfo)
            {
                if ($awardInfo['type'] == 1) // 奖品类型  1=>礼物 2=>优惠券
                {
                    $gids[] = (int)$awardInfo['cid'];
                } else
                {
                    $cids[] = (int)$awardInfo['cid'];
                }
            }

            $gidData = [];
            if ($gids)
            {
                $gidData = $this->table('wechat_backup_07_18_14_43_00.live_gift')->whereIn('id', $gids)->column('name,coin', 'id');
            }
            $cidData = [];
            if ($cids)
            {
                $cidData = $this->table('wechat_backup_07_18_14_43_00.zl_coupon_attr')->whereIn('id', $cids)->column('name,desc', 'id');
            }


            $resData = [];

            foreach ($data as $aId => $awardInfo)
            {
                if (isset($gidData[$awardInfo['cid']]['coin']))
                {
                    if ($gidData[$awardInfo['cid']]['coin'])
                    {
                        $awardDesc = $gidData[$awardInfo['cid']]['coin'] . '准币';
                    } else
                    {
                        $awardDesc = ' 免费';
                    }
                }
                $resData[$awardInfo['id']] = [
                    'id' => $awardInfo['id'],
                    'name' => $awardInfo['name'],
                    'type' => $awardInfo['type'],
                    'status' => $awardInfo['status'],
                    'cid' => $awardInfo['cid'],
                    'award_name' => $awardInfo['type'] == 1 ? $gidData[$awardInfo['cid']]['name'] ?? '' : $cidData[$awardInfo['cid']]['desc'] ?? '',
                    'award_desc' => $awardInfo['type'] == 1 ? $awardDesc ?? '' : $cidData[$awardInfo['cid']]['name'] ?? '',
                ];
            }

            return $resData;
        }
        catch (\Exception $e)
        {
            Log::write("抽奖奖品获取失败：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            return [];
        }
    }
}
